/*
 * @Author: your name
 * @Date: 2020-09-18 11:36:06
 * @LastEditTime: 2021-01-23 16:59:36
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \egg-RESTfulAPI\app\controller\virtualEtc.js
 */
/*
 * @Author: your name
 * @Date: 2020-09-18 11:36:06
 * @LastEditTime: 2020-09-18 15:18:21
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \egg-RESTfulAPI\app\controller\virtualEtc.js
 */
const Controller = require('egg').Controller
let moment = require('moment');
class ETCController extends Controller {
    constructor(ctx) {
        super(ctx)
        this.listRule = {
            current: { type: 'int', required: true, allowEmpty: false },
            size: { type: 'int', required: true, allowEmpty: false }
        }
        this.updateRule = {
            _id: { type: 'string', required: true, allowEmpty: false },
            // isDefault: { type: 'string', required: false, allowEmpty: false },
            mapName: { type: 'string', required: true, allowEmpty: false }
        }
        this.removeRule = {
            ids: { type: 'string', required: true, allowEmpty: false },
        }
        this.detailRule = {
            id: { type: 'string', required: true, allowEmpty: false },
        }

    }

    async list() {
        const { ctx } = this
        ctx.validate(this.listRule)
        let { current, size } = ctx.request.body
        current = parseInt(current) - 1
        size = parseInt(size)
        let totalCount = await this.ctx.model.BaseSegmentAndPoinManagertModel.find({}).count()
        let managerSces = await this.ctx.model.BaseSegmentAndPoinManagertModel.find({}).sort({ createdAt: 1 }).skip(current * size).limit(size)
        ctx.helper.success({ ctx, res: { records: managerSces, total: totalCount } })
    }
    async update() {
        const { ctx } = this
        ctx.validate(this.updateRule)
        let { _id, isDefault, mapName } = ctx.request.body

        let selectMap = await this.ctx.model.BaseSegmentAndPoinManagertModel.findOne({ '_id': _id })
        if (!selectMap) {
            ctx.helper.InvaidRequest({ ctx, msg: "无该id" })
            return
        }
        if (isDefault == 'true') {
            await this.ctx.model.BaseSegmentAndPoinManagertModel.update({ isDefault: true }, { isDefault: false })
            selectMap.isDefault = true
        }
        selectMap.mapName = mapName
        let saveData = await selectMap.save()
        ctx.helper.success({ ctx, res: saveData })
    }
    async remove() {
        const { ctx } = this
        ctx.validate(this.removeRule)
        let { ids } = ctx.request.body
        ids = ids.split(',')
        let depandInfo = await this.ctx.model.PlanManagerModel.find({ mapId: { $in: ids } })
        if (depandInfo.length != 0) {
            ctx.helper.InvaidRequest({ ctx, msg: "存在被依赖的路段，删除失败！" })
            return
        }
        await this.ctx.model.BasePointModel.remove({ manager: { $in: ids } })
        await this.ctx.model.BaseSegmentAndPoinManagertModel.remove({ _id: { $in: ids } })
        await this.ctx.model.BaseSegmentModel.remove({ manager: { $in: ids } })

        ctx.helper.success({ ctx, res: {} })
    }

    async detail() {
        const { ctx } = this
        ctx.validate(this.detailRule)
        let { id } = ctx.request.body
        let result = await this.ctx.model.BaseSegmentAndPoinManagertModel.findOne({ _id: id })
        ctx.helper.success({ ctx, res: result })
    }






}


module.exports = ETCController